﻿package net.wangit.dev.group;

import net.wangit.Unit;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionContextHelper;
import net.wangit.dev.ConsoleDomainUtil;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.util.DBHelper;
import net.wangit.support.UnitInterceptor;
import net.wangit.util.Checker;

public class GroupUI implements UnitInterceptor {

	public static final String KEY_ID = "net.wangit.dev.group.GroupUI.groupId";
	public static final String KEY_TYPE = "net.wangit.dev.group.GroupUI.groupType";
	private static final String SQL = "select * from CGROUP where gid=? and t=?";

	public GroupUI() {
	}

	public String intercept(ActionContext ac, Unit unit) throws Exception {
		String groupType = ActionContextHelper.getRequest2Session(ac,
				"grouptype", GroupUI.KEY_TYPE, null);
		unit.getElement("t").setDefaultValue(groupType);
		if (unit.getData() == null && !"a".equals(unit.getShowMode())) {
			String domain = ConsoleDomainUtil.getDomain(ac);
			String groupId = ActionContextHelper.getRequest2Session(ac,
					"groupid", GroupUI.KEY_ID, null);
			if (!Checker.isEmpty(domain) && !Checker.isEmpty(groupId)
					&& !Checker.isEmpty(groupType)) {
				DB db = DBHelper.getDomainDB(domain);
				String values[] = { groupId, groupType };
				int types[] = { 2, 12 };
				unit.setData(db.query(GroupUI.SQL, values, types));
			}
		}
		return unit.write(ac);
	}
}
